<template>
  <div class="c-bg-sgray theme-market">
    <div class="c-bg-white c-pb32">
      <div>
        <collage-item :collageList="collageList"></collage-item>
      </div>
    </div>
    <loading-status-tem :noDataText="'暂无结果'" :noData="collageList.length == 0" :noMoreData="collageList.length > 0 && is_page == false" :showLoading="isLoading"></loading-status-tem>
    <div v-if="companyAuth != null && companyAuth.companyNav != null">
      <nav-bottom-bar :itemIndex="10" v-for="(item,index) in companyAuth.companyNav" :key="index" v-if="item.navType == 10"></nav-bottom-bar>
    </div>
  </div>
</template>

<script>
import CollageItem from "@/components/templates/collage/collageItem.vue";
import loadingStatusTem from "@/components/templates/common/loadingStatusTem.vue";
import NavBottomBar from "@/components/templates/common/navBottomBar.vue";
import { utilJs } from "@/utils/common.js"
export default {
  name: "CollageList",
  components: {
    loadingStatusTem,
    CollageItem,
    NavBottomBar,
  },
  props: [],
  data() {
    return {
      cfrom: global.ckFrom,
      theme: localStorage.getItem("colorName") ? localStorage.getItem("colorName") : 'mb5_default',
      autofocus: false,
      is_page: false,
      page: 1,
      limit: 10,
      searchText: '',
      collageList: [],
      isLoading: false,
      companyAuth: localStorage.getItem("companyAuth") ? JSON.parse(localStorage.getItem("companyAuth")) : null,
      prodType: '',
      isInitDate: false,
    };
  },
  created() {
    utilJs.appShare(this);
  },
  watch: {
    $route: function (to, from) {
      this.fId = this.$route.query.fId || null;
      if ((from.path == '/' || from.path == '/homePage/microPage/microPage') && to.path == '/homePage/collage/collageList') {
        this.isInitDate = true
      } else {
        this.isInitDate = false
      }
    }
  },
  mounted() {

  },
  methods: {
    //通过名称模糊搜索拼团
    // startSearch(e) {
    //   this.page = 1;
    //   this.collageList = [];
    //   utilJs.toPageScrollTop();
    //   this.searchText = e.searchValue;
    //   this.getMore();
    // },
    //分页获取拼团列表
    getMore() {
      if (!this.isLoading) {
        this.isLoading = true;
        let $this = this;
        utilJs.getMethod(global.apiurl + 'collage/getCollageList?prodName=' + this.searchText + '&page=' + this.page + '&limit=' + this.limit + '&prodType=' + this.prodType, function (res) {
          $this.companyAuth = JSON.parse(localStorage.getItem("companyAuth"));
          if ($this.page == 1) {
            $this.collageList = res.data;
          } else {
            let list = res.data;
            $this.collageList = $this.collageList.concat(list);
          }
          $this.page = res.data && (res.data.length >= $this.limit) ? ($this.page + 1) : $this.page;//res.current_page < res.last_page ? ($this.page + 1) : $this.page;
          $this.is_page = res.data && (res.data.length >= $this.limit) ? true : false;//res.current_page < res.last_page ? true : false;

          $this.isLoading = false;
        });
      }
    },
    //分享
    wechatShare: function () {
      let title = "特惠专区";//window.localStorage.getItem("shareTitle");
      let shareUrl =
        `${window.location.href.split("#")[0]}#${this.$route.path}?refereeId=${localStorage.getItem("userId")}&prodType=${this.prodType}`;
      utilJs.wechatConfig(shareUrl, title, "", "", function () { });
    },
    //手机端分享
    appShare: function () {
      let title = "特惠专区";//window.localStorage.getItem("shareTitle");
      let shareUrl =
        `${window.location.href.split("#")[0]}#${this.$route.path}?refereeId=${localStorage.getItem("userId")}&prodType=${this.prodType}`;
      utilJs.appShareTrue(title, "", shareUrl, "");
    },
    //下滑加载更多
    windowScrollFunction: function () {
      let that = this;
      utilJs.handleLoading(function () {
        if (that.is_page == true) {
          that.getMore();
        }
      });
    },
    initData() {
    // pageIndex = 0;
      this.searchText = '';
      this.page = 1;
      this.is_page = false;
    // this.dataStatus = 'HAS_MORE_DATA';
      this.collageList = [];
      this.getMore();
    }
  },

  activated() {
    this.prodType = this.$route.query.prodType || '';
    //拼团列表
    setDocumentTitle("特惠专区");// eslint-disable-line
    if (utilJs.checkNeedInitData(this.collageList, this.page) || this.isInitDate) {
      // this.getMore();
      this.initData()
    }
    this.wechatShare();
    this.isLoading = false;
    window.addEventListener('scroll', this.windowScrollFunction);
  },
  deactivated() {
    // this.is_page = false;
    window.removeEventListener('scroll', this.windowScrollFunction);
  }
};
</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->

<style scoped>
</style>
